package com.wnx.job.component;


import com.wnx.job.common.api.Const;
import com.wnx.job.modules.jms.model.JmsCompany;
import com.wnx.job.modules.jms.service.JmsCompanyService;
import com.wnx.job.modules.pms.model.PmsEmail;
import com.wnx.job.modules.pms.service.PmsEmailService;
import com.wnx.job.modules.ums.model.UmsAdmin;
import com.wnx.job.modules.ums.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * @author by wangnaixing
 * @Description
 * @Date 2021/12/18 14:00
 */
@Slf4j
@Component
public class AsyncTask {
    @Resource
    private JmsCompanyService companyService;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private PmsEmailService emailService;

    /**
     * 修改给企业发送邮件的状态
     * @param tbCompany
     * @param
     * @return
     */
    @Async
    public void updateEmailStatus(JmsCompany tbCompany){
        //调用服务层修改邮件发送状态为已发送
        tbCompany.setEmailStatus(Const.SEND_COMPANY_PASS_EMAIL_YES);
        //执行修改状态操作
        companyService.updateById(tbCompany);
    }

    /**
     * 给企业发送邮件
     * @param tbCompany
     * @param
     * @return
     */
    @Async
    public void sendEmail(JmsCompany tbCompany, Principal principal){
        //从前端获取当前审核通过企业的ID


        //调用服务层修改邮件发送状态为已发送
        PmsEmail mail = new PmsEmail();
        mail.setFromPeople("gxuwz0722@163.com");
        mail.setToPeople(tbCompany.getEmail());
        mail.setSubject("尊敬的【"+tbCompany.getName()+"】企业，感谢您成为梧州学院官方合作企业！");
        mail.setText("您的账号为"+tbCompany.getAccount()+"初始化密码为：888888，请务必登录就业信息网查看最新信息，发布相关职位信息，完善企业信息以及修改初始密码和学校负责人取得联系，如学校有关就业事宜将通过电话联系您！");
        mail.setCompanyId(tbCompany.getId());

         UmsAdmin admin = adminService.getAdminByUsername(principal.getName());
        mail.setWorkerId(admin.getId());

        //调用服务层，给学生发送简历通过筛选邮件
        emailService.sendMail(mail);


    }


}
